﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QualityRpt.Service;
using System.Collections;
using QualityRpt.Utils;

namespace QualityRPT.OBUContract
{
    public partial class index : System.Web.UI.Page
    {
        public string PagerHtml = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
				this.h_dev.Value = DeviceService.OBU_DEV.ToString();
                DataTable spTable = DeviceSupplierService.GetListSupplierName(string.Empty, string.Empty, DeviceService.OBU_DEV);
                this.ajax_spin.DataSource = spTable;
                this.ajax_spin.DataTextField = "SUPPLIER_NAME"; this.ajax_spin.DataValueField = "ID";
                this.ajax_spin.DataBind();

                DataTable cnTable = DeviceContractDetailService.GetListBySpIn(DeviceService.OBU_DEV, string.Empty,null);
                this.ajax_cnin.DataSource = cnTable;
                this.ajax_cnin.DataTextField = "CONTRACT_NO"; this.ajax_cnin.DataValueField = "CONTRACT_NO";
                this.ajax_cnin.DataBind();
                GetParams();
                BindData();
            }
        }
        public string strSpIn = string.Empty; public string strCnIn = string.Empty;
        public string strPage = string.Empty;
        private void GetParams()
        {
            strSpIn = Request.Params["spin"];
            strCnIn = Request.Params["cnin"];
            strPage = Request.Params["p"];
            if (strPage == null)
            {
                strPage = string.Empty;
            }
        }
        private void PostParams()
        {
            strSpIn = Request.Params["ajax_spin"];
            strCnIn = Request.Params["ajax_cnin"];
            if (strSpIn == null) { strSpIn = string.Empty; }
            if (strCnIn == null) { strCnIn = string.Empty; }
        }
        private void BindParams()
        {
            if (strPage == string.Empty)
            {
                strPage = "1";
            }
            //厂商选中状态
            if (strSpIn != null && strSpIn.Trim() != string.Empty)
            {
                string[] selSpin = strSpIn.Split(new char[] { ',' });
                Hashtable hashSelSp = new Hashtable();
                for (int i = 0; i < selSpin.Length; i++)
                {
                    hashSelSp.Add(selSpin[i], 0);
                }
                for (int i = 0; i < ajax_spin.Items.Count; i++)
                {
                    if (hashSelSp.ContainsKey(ajax_spin.Items[i].Value))
                    {
                        ajax_spin.Items[i].Selected = true;
                    }
                }
            }
            //合同选中状态
            if (strSpIn != null && strSpIn.Trim() != string.Empty)
            {
                DataTable cntable = DeviceContractDetailService.GetListBySpIn(DeviceService.OBU_DEV, strSpIn,null);
                this.ajax_cnin.DataSource = cntable;
                this.ajax_cnin.DataTextField = "CONTRACT_NO"; this.ajax_cnin.DataValueField = "CONTRACT_NO";
                this.ajax_cnin.DataBind();
                if (strCnIn != null && strCnIn.Trim() != string.Empty)
                {
                    string[] selCnin = strCnIn.Split(new char[] { ',' });
                    Hashtable hashSelCn = new Hashtable();
                    for (int i = 0; i < selCnin.Length; i++)
                    {
                        hashSelCn.Add(selCnin[i], 0);
                    }
                    for (int i = 0; i < ajax_cnin.Items.Count; i++)
                    {
                        if (hashSelCn.ContainsKey(ajax_cnin.Items[i].Value))
                        {
                            ajax_cnin.Items[i].Selected = true;
                        }
                    }
                }
            }
        }
        protected void ButtonQ_Click(object sender, EventArgs e)
        {
            PostParams();
            BindData();
        }
        private void BindData()
        {
            BindParams();
            PageUtils pu = new PageUtils(); pu.PerPageNum = 20;
            pu.Page = int.Parse(strPage);
            DataTable detailTable = null;
            if (strCnIn == string.Empty && strSpIn != string.Empty)
            {
                detailTable = DeviceContractDetailService.GetListBySpIn(DeviceService.OBU_DEV, strSpIn,pu);
            }
            else
            {
                detailTable = DeviceContractDetailService.GetListByCnIn(DeviceService.OBU_DEV, strCnIn,pu);
            }
            this.repeater_data.DataSource = detailTable;
            this.repeater_data.DataBind();
            PagerHtml = pu.GenStPagination("index.aspx?pid="+Request.Params["pid"]+"&cid="+Request.Params["cid"]+"&p={page}&spin="+strSpIn+"&cnin="+strCnIn);
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("edit.aspx?pid="+Request.Params["pid"]+"&cid="+Request.Params["cid"]);
        }
    }
}